package com.ke.jdyd.ui.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ke.jdyd.R;
import com.ke.jdyd.model.GoodsInfo;
import com.ke.jdyd.util.GlideApp;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  private Context mContext;
  private List<GoodsInfo> mDatas = new ArrayList<>();
  private int mWidth;
  private ItemClickListener mItemClickListener;
  private View.OnClickListener onClickListener;

  public HomeAdapter(Context context, List<GoodsInfo> datas) {
    this.mContext = context;
    this.mDatas.addAll(datas);
    mWidth = (getScreenWidth(context) - mContext.getResources().getDimensionPixelSize(R.dimen.jdyd_dimen_32_dp)) / 2;
  }

  public void setOnClickListener(View.OnClickListener onClickListener) {
    this.onClickListener = onClickListener;
  }

  @NonNull
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_home_goods, parent, false));
  }

  @Override
  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    final GoodsInfo entity = mDatas.get(position);
    GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
    params.width = mWidth;
    if (position % 2 == 0) {
      params.leftMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.jdyd_dimen_12_dp);
      params.rightMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.jdyd_dimen_4_dp);
    } else {
      params.rightMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.jdyd_dimen_12_dp);
      params.leftMargin = mContext.getResources().getDimensionPixelOffset(R.dimen.jdyd_dimen_4_dp);
    }
    if (entity != null) {
      ViewHolder viewHolder = (ViewHolder) holder;
      viewHolder.desc.setText(entity.getGoods_name());
      viewHolder.price.setText(String.format("￥%s", entity.getPrice()));
      GlideApp.with(mContext)
          .load(entity.getGoods_cover())
          .override(mWidth
              , mWidth)
          .into(viewHolder.img);

      viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          // TODO: 2021/1/23   跳转到商品详情页 id
          // 调用商品详情的接口
//          ===============
          if (mItemClickListener != null) {
            mItemClickListener.onItemClick(entity.getId());
          }


        }
      });

    }

  }

  public void setItemClickListener(ItemClickListener itemClickListener) {
    this.mItemClickListener = itemClickListener;
  }

  @Override
  public int getItemCount() {
    return mDatas.size();
  }

  class ViewHolder extends RecyclerView.ViewHolder {
    ImageView img;
    TextView desc;
    TextView price;

    public ViewHolder(@NonNull View itemView) {
      super(itemView);
      img = itemView.findViewById(R.id.item_good_img);
      desc = itemView.findViewById(R.id.item_good_desc);
      price = itemView.findViewById(R.id.item_good_price);
    }
  }

  /**
   * 获得屏幕宽度
   */
  public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
  }
 public interface ItemClickListener{
    void onItemClick(String id);
 }
}
